//
//  ImageActionBarView.m
//  ImageEnginePlugin
//
//  Created by VNG on 10/18/12.
//
//

#import "ImageActionBarView.h"
#import "Utils.h"


@implementation ImageActionBarView

@synthesize isCancelEffect;
@synthesize leftButton;
@synthesize rightButton;

-(id)initWithKindButton:(NSInteger)_number
            spaceButton:(NSInteger)_space
            imageNormal:(NSArray*)_arrayImage
         imagesSelected:(NSArray *)_arrayImageSelect
             sizeButton:(CGSize)_sizeButton
                  frame:(CGRect)_frame
        imageBackground:(UIImage*)_image
               delegate:(NSObject <ImageActionBarViewDelegate> *)_otherDelegate
{
    if ((self = [super init]))
    {
        self.frame = _frame;
        
        [self setCanCancelContentTouches:NO];
        self.showsHorizontalScrollIndicator = FALSE;
        self.showsVerticalScrollIndicator = FALSE;
        self.clipsToBounds = YES;
        self.scrollEnabled = FALSE;
        self.zoomScale = FALSE;
        
        frameSelfNormal = _frame;
        frameMainNormal = CGRectMake(0, 0, _frame.size.width, _frame.size.height);
        
        UIView* mainview = [[UIView alloc] initWithFrame:frameMainNormal];
        
        if(_image != NULL)
        {
            UIGraphicsBeginImageContext(self.frame.size);
            [_image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
            UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
            [mainview setBackgroundColor:[UIColor colorWithPatternImage:newImage]];
        }else{
            [mainview setBackgroundColor:[UIColor darkGrayColor]];
        }
        
        UIView* TopView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
        
        kindActionBar = kActionBarModeButton;
        numButton = _number;
        delegate = _otherDelegate;
        sizeButton = _sizeButton;
        
        int YButton = (_frame.size.height - sizeButton.height)/2;
        
        [self addLeftRightButton:mainview mainFrame:_frame];
        
        arrayKindButton = [[NSMutableArray alloc] init];
        arrayIndexBarItemInSelfView = [[NSMutableArray alloc] init];
        
        CGFloat kSpaceWithOtherButton = _space;
        CGFloat widthAllButton = numButton*sizeButton.width + (numButton - 1)*kSpaceWithOtherButton;
        CGFloat curXButton = _frame.size.width/2 - widthAllButton/2;
        
        for (int i = 0; i < numButton; i++) {
            UIButton *button = [[UIButton  alloc] init];
            
            CGRect frameButton = CGRectMake(curXButton, YButton, sizeButton.width, sizeButton.height);
            button.frame = frameButton;
            
            if (_arrayImage != NULL) {
                UIImage* imageButtonNormal = (UIImage * )[_arrayImage objectAtIndex:i];
                if (imageButtonNormal != nil) {
                    [button setImage:imageButtonNormal forState:UIControlStateNormal];
                }
            }
            
            if (_arrayImageSelect != NULL) {
                UIImage* imageButtonSelected = (UIImage * )[_arrayImageSelect objectAtIndex:i];
                if (imageButtonSelected != nil) {
                    [button setImage:imageButtonSelected forState:UIControlStateSelected];
                }
            }
            
            button.tag = i + 2;
            [button addTarget:self action:@selector(buttonSelect:) forControlEvents:UIControlEventTouchUpInside];
            
            [mainview addSubview:button];
            [button release];
            
            curXButton = curXButton + (sizeButton.width + kSpaceWithOtherButton);
            
            [arrayKindButton addObject:[NSNumber numberWithInt:kKindButtonActionAction]];
            [arrayIndexBarItemInSelfView addObject:[NSNumber numberWithInt:-1]];
        }
        
        [self addSubview:mainview];
        [self addSubview:TopView];
        
        [TopView release];
        [mainview release];
        
        isEffectIn = false;
    }
    return self;
}

-(id)initWithKindSegmentButton:(NSInteger)_num
                         names:(NSArray*)_arrayName
                   imageNormal:(NSArray*)_arrayImage
                imagesSelected:(NSArray *)_arrayImageSelect
                    sizeButton:(CGSize)_sizeButton
                         frame:(CGRect)_frame
               imageBackground:(UIImage*)_image
                      delegate:(NSObject <ImageActionBarViewDelegate> *)_otherDelegate
{
    if ((self = [super init]))
    {
        self.frame = _frame;
        
        [self setCanCancelContentTouches:NO];
        self.showsHorizontalScrollIndicator = FALSE;
        self.showsVerticalScrollIndicator = FALSE;
        self.clipsToBounds = YES;
        self.scrollEnabled = FALSE;
        self.zoomScale = FALSE;
        
        frameSelfNormal = _frame;
        frameMainNormal = CGRectMake(0, 0, _frame.size.width, _frame.size.height);
        
        UIView* mainview = [[UIView alloc] initWithFrame:frameMainNormal];
        
        if(_image != NULL)
        {
            [mainview setBackgroundColor:[UIColor colorWithPatternImage:_image]];
        }else{
            [mainview setBackgroundColor:[UIColor darkGrayColor]];
        }
        
        UIView* TopView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
        
        kindActionBar = kActionBarModeSegmentButton;
        numButton = _num;
        delegate = _otherDelegate;
        sizeButton = _sizeButton;
        
        int YButton = (_frame.size.height - sizeButton.height)/2;
        
        [self addLeftRightButton:mainview mainFrame:_frame];
        
        arrayKindButton = [[NSMutableArray alloc] init];
        arrayIndexBarItemInSelfView = [[NSMutableArray alloc] init];
        
        if (_arrayName != NULL) {
            UISegmentedControl *mainMenu = [[UISegmentedControl alloc] initWithItems:_arrayName];
            mainMenu.segmentedControlStyle = UISegmentedControlStyleBar;

            for (int i = 0; i < [_arrayName count]; i++) {
                [[[mainMenu subviews] objectAtIndex:i] setTintColor:[UIColor grayColor]];
            }
            int xMainMenu = _frame.size.width/2 - (numButton*sizeButton.width)/2;
            mainMenu.frame = CGRectMake(xMainMenu, YButton, numButton*sizeButton.width, sizeButton.height);
            
            [mainMenu addTarget:self
                         action:@selector(segmentChanged:)
               forControlEvents:UIControlEventValueChanged];
            
            [mainview addSubview:mainMenu];
            [mainMenu release];
        }else{
            if (_arrayName != NULL) {
                UISegmentedControl *mainMenu = [[UISegmentedControl alloc] initWithItems:nil];
                for (int i = 0; i < numButton; i++) {
                    UIImage* image = (UIImage *)[_arrayName objectAtIndex:i];
                    [mainMenu insertSegmentWithImage:image atIndex:i animated:NO];
                }
                int xMainMenu = _frame.size.width/2 - (numButton*sizeButton.width)/2;
                mainMenu.frame = CGRectMake(xMainMenu, YButton, numButton*sizeButton.width, sizeButton.height);
                
                [mainMenu addTarget:self
                             action:@selector(segmentChanged:)
                   forControlEvents:UIControlEventValueChanged];
                [mainview addSubview:mainMenu];
                [mainMenu release];
            }
        }
        
        for (int i = 0; i < numButton; i++) {
            [arrayKindButton addObject:[NSNumber numberWithInt:kKindButtonActionAction]];
            [arrayIndexBarItemInSelfView addObject:[NSNumber numberWithInt:-1]];
        }
        
        [self addSubview:mainview];
        [self addSubview:TopView];
        
        [TopView release];
        [mainview release];
        
        isEffectIn = false;
    }
    
    return self;
}

-(void)addLeftRightButton:(UIView*) mainview mainFrame:(CGRect)frame
{
    //Add left button
    UIImage* leftImage = [UIImage imageNamed:@"x.png"];
    UIImage* leftImageSelected = [UIImage imageNamed:@"x1.png"];
    leftButton = [[UIButton alloc] initWithFrame:CGRectMake(kLeftSpace, (frame.size.height - leftImage.size.height)/2, leftImage.size.width, leftImage.size.height)];
    [leftButton setImage:leftImage forState:UIControlStateNormal];
    [leftButton setImage:leftImageSelected forState:UIControlStateSelected];
    leftButton.tag = kTagLeft;
    [leftButton addTarget:self action:@selector(buttonSelect:) forControlEvents:UIControlEventTouchUpInside];
    [mainview addSubview:leftButton];
    [leftButton release];
    
    //Add Right Button
    UIImage* rightImage = [UIImage imageNamed:@"check.png"];
    UIImage* rightImageSelected = [UIImage imageNamed:@"check1.png"];
    rightButton = [[UIButton alloc] initWithFrame:CGRectMake(frame.size.width - kLeftSpace - rightImage.size.width, (frame.size.height                    - rightImage.size.height)/2, rightImage.size.width, rightImage.size.height)];
    [rightButton setImage:rightImage forState:UIControlStateNormal];
    [rightButton setImage:rightImageSelected forState:UIControlStateSelected];
    rightButton.tag = kTagRight;
    [rightButton addTarget:self action:@selector(buttonSelect:) forControlEvents:UIControlEventTouchUpInside];
    [mainview addSubview:rightButton];
    [rightButton release];
}

-(void)setItemSelectedAtIndex:(NSInteger)index
{
    if (kindActionBar == kActionBarModeButton){
        if (index < numButton) {
            UIView* mainView = (UIView* )[[self subviews] objectAtIndex:0];
            UIButton* button = (UIButton*)[[mainView subviews] objectAtIndex:(index + 2)];
            [button setSelected:true];
        }
    }
    if (kindActionBar == kActionBarModeSegmentButton) {
        if (index < numButton) {
            UIView* mainView = (UIView* )[[self subviews] objectAtIndex:0];
            UISegmentedControl *mainMenu = (UISegmentedControl *)[[mainView subviews] objectAtIndex:2];
            [mainMenu setSelectedSegmentIndex:index];
        }
    }
}

-(void)buttonSelect:(id)sender
{
    itemSelected = -1;
    UIButton* button = (UIButton *)sender;
    if (([button isKindOfClass:[UIButton class]]) &&(button.tag >= 0)) {
        itemSelected = button.tag;
        
        if ((button.tag == kTagLeft)||(button.tag == kTagRight)) {
            if ([delegate respondsToSelector:@selector(finishedActionSelected:indexSelect:)])
                [delegate finishedActionSelected:self indexSelect:itemSelected];
        }else{
            if ([delegate respondsToSelector:@selector(finishedActionSelected:indexSelect:)])
                [delegate finishedActionSelected:self indexSelect:itemSelected];
        }
    }
}

-(void)upDateRightButtonState:(bool)enable
{
    UIView* mainView = (UIView* )[[self subviews] objectAtIndex:0];
    
    UIButton* rightButton = (UIButton *)[[mainView subviews] objectAtIndex:kTagRight];
    
    [rightButton setUserInteractionEnabled:enable];
}

- (void)updateImageButtonAtIndex:(int)index
                     imageButton:(UIImage*)image
{
    if (kindActionBar == kActionBarModeButton){
        if (index < numButton) {
            if ([[self subviews] count] > 0) {
                UIView* mainView = (UIView* )[[self subviews] objectAtIndex:0];
                if ((index + 2) < [[mainView subviews] count]) {
                    UIButton* button = (UIButton*)[[mainView subviews] objectAtIndex:(index + 2)];
                    
                    [button setImage:image forState:UIControlStateNormal];
                    [button setImage:image forState:UIControlStateSelected];
                }
            }
        }
    }
    if (kindActionBar == kActionBarModeSegmentButton){
    }
}

- (void)updateBackgroundImageButtonAtIndex:(int)index
                               imageButton:(UIImage*)image
{
    if (kindActionBar == kActionBarModeButton){
        if (index < numButton) {
            if ([[self subviews] count] > 0) {
                UIView* mainView = (UIView* )[[self subviews] objectAtIndex:0];
                if ((index + 2) < [[mainView subviews] count]) {
                    UIButton* button = (UIButton*)[[mainView subviews] objectAtIndex:(index + 2)];
                    
                    [button setBackgroundImage:image forState:UIControlStateNormal];
                    [button setBackgroundImage:image forState:UIControlStateSelected];
                }
            }
        }
    }
    if (kindActionBar == kActionBarModeSegmentButton){
    }
}

- (void)slideValueChange:(NSInteger)newValue
{
    if ([delegate respondsToSelector:@selector(slideValueChange: atIndexSelect:)])
        [delegate slideValueChange:newValue atIndexSelect:itemSelected];
}

- (void)finishedAnimationWhenTouchButton:(NSInteger)iIndexSelected
{
    itemSubSelected = iIndexSelected;
    
    if ([delegate respondsToSelector:@selector(finishedActionSubItemSelected: indexSelect: indexSubItemSelect:)]) {
        [delegate finishedActionSubItemSelected:self
                                    indexSelect:itemSelected
                             indexSubItemSelect:itemSubSelected];
    }
}

-(void)dealloc
{
    if (arrayKindButton) {
        [arrayKindButton removeAllObjects];
        UX_RELEASE_SAFELY(arrayKindButton);
    }
    if (arrayIndexBarItemInSelfView) {
        [arrayIndexBarItemInSelfView removeAllObjects];
        UX_RELEASE_SAFELY(arrayIndexBarItemInSelfView);
    }
    
    [super dealloc];
}

- (UIButton*)buttonAtIndex:(int)index{
    UIView* mainView = (UIView* )[[self subviews] objectAtIndex:0];
    UIButton* button = (UIButton*)[[mainView subviews] objectAtIndex:(index + 2)];
    return button;
}

- (void) setFrame:(CGRect)frame forButtonIndex:(int)index{
    if (index < numButton) {
        if ([[self subviews] count] > 0) {
            UIView* mainView = (UIView* )[[self subviews] objectAtIndex:0];
            if ((index + 2) < [[mainView subviews] count]) {
                UIButton* button = (UIButton*)[[mainView subviews] objectAtIndex:(index + 2)];
                
                button.frame = frame;
            }
        }
    }
}

- (CGRect) frameOfButtonIndex:(int)index{
    if (index < numButton) {
        if ([[self subviews] count] > 0) {
            UIView* mainView = (UIView* )[[self subviews] objectAtIndex:0];
            if ((index + 2) < [[mainView subviews] count]) {
                UIButton* button = (UIButton*)[[mainView subviews] objectAtIndex:(index + 2)];
                return button.frame;
            }
        }
    }
    return CGRectMake(0, 0, 0, 0);
}

- (void)setLeftButton:(UIButton *)_leftButton{
    if(leftButton){
        [leftButton removeFromSuperview];
        [leftButton release];
        leftButton = [_leftButton retain];
    
        leftButton.frame = CGRectMake(kLeftSpace, (self.frame.size.height - leftButton.frame.size.height)/2, leftButton.frame.size.width, leftButton.frame.size.height);
        leftButton.tag = kTagLeft;
        [leftButton addTarget:self action:@selector(buttonSelect:) forControlEvents:UIControlEventTouchUpInside];
    }
    else{
        leftButton.hidden = true;
    }
}

- (void)setRightButton:(UIButton *)_rightButton{
    if(_rightButton){
        [rightButton removeFromSuperview];
        [_rightButton release];
        rightButton = [_rightButton retain];
        rightButton.frame = CGRectMake(self.frame.size.width - kLeftSpace - rightButton.frame.size.width, (self.frame.size.height                    - rightButton.frame.size.height)/2, rightButton.frame.size.width, rightButton.frame.size.height);
        rightButton.tag = kTagRight;
        [leftButton addTarget:self action:@selector(buttonSelect:) forControlEvents:UIControlEventTouchUpInside];
    }
    else{
        rightButton.hidden = true;
    }
}

@end
